home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Tool Chest / Development Tools & Languages / Dylan Related / Mindy / Mindy 1.2 - portable sources / comp / sym.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-15  |  7.4 KB  |  273 lines  |  [TEXT/ttxt]

  1. /**********************************************************************\
  2. *
  3. *  Copyright (c) 1994  Carnegie Mellon University
  4. *  All rights reserved.
  5. *  
  6. *  Use and copying of this software and preparation of derivative
  7. *  works based on this software are permitted, including commercial
  8. *  use, provided that the following conditions are observed:
  9. *  
  10. *  1. This copyright notice must be retained in full on any copies
  11. *     and on appropriate parts of any derivative works.
  12. *  2. Documentation (paper or online) accompanying any system that
  13. *     incorporates this software, or any part of it, must acknowledge
  14. *     the contribution of the Gwydion Project at Carnegie Mellon
  15. *     University.
  16. *  
  17. *  This software is made available "as is".  Neither the authors nor
  18. *  Carnegie Mellon University make any warranty about the software,
  19. *  its performance, or its conformity to any specification.
  20. *  
  21. *  Bug reports, questions, comments, and suggestions should be sent by
  22. *  E-mail to the Internet address "gwydion-bugs@cs.cmu.edu".
  23. *
  24. ***********************************************************************
  25. *
  26. * $Header: sym.c,v 1.9 94/10/05 20:56:10 nkramer Exp $
  27. *
  28. * This file implements symbols.
  29. *
  30. \**********************************************************************/
  31.  
  32. #include "../compat/std-c.h"
  33.  
  34. #include <ctype.h>
  35.  
  36. #include "mindycomp.h"
  37. #include "sym.h"
  38.  
  39. struct symbol *sym_DefineClass1 = NULL;
  40. struct symbol *sym_DefineClass2 = NULL;
  41. struct symbol *sym_DefineGeneric = NULL;
  42. struct symbol *sym_DefineMethod = NULL;
  43. struct symbol *sym_DefineSlot = NULL;
  44. struct symbol *sym_Or = NULL;
  45. struct symbol *sym_Plus = NULL;
  46. struct symbol *sym_Less = NULL;
  47. struct symbol *sym_LessEqual = NULL;
  48. struct symbol *sym_Object = NULL;
  49. struct symbol *sym_Type = NULL;
  50. struct symbol *sym_Eq = NULL;
  51. struct symbol *sym_DylanUser = NULL;
  52. struct symbol *sym_Apply = NULL;
  53. struct symbol *sym_Aref = NULL;
  54. struct symbol *sym_Catch = NULL;
  55. struct symbol *sym_CheckType = NULL;
  56. struct symbol *sym_Class = NULL;
  57. struct symbol *sym_Constant = NULL;
  58. struct symbol *sym_Do = NULL;
  59. struct symbol *sym_Element = NULL;
  60. struct symbol *sym_Error = NULL;
  61. struct symbol *sym_FindVariable = NULL;
  62. struct symbol *sym_ForwardIterationProtocol = NULL;
  63. struct symbol *sym_Getter = NULL;
  64. struct symbol *sym_InitVariable = NULL;
  65. struct symbol *sym_Instance = NULL;
  66. struct symbol *sym_List = NULL;
  67. struct symbol *sym_MakeInherited = NULL;
  68. struct symbol *sym_MakeInitarg = NULL;
  69. struct symbol *sym_MakeNextMethodFunction = NULL;
  70. struct symbol *sym_MakeSlot = NULL;
  71. struct symbol *sym_Negative = NULL;
  72. struct symbol *sym_NegativeP = NULL;
  73. struct symbol *sym_NextMethod = NULL;
  74. struct symbol *sym_PopHandler = NULL;
  75. struct symbol *sym_PushHandler = NULL;
  76. struct symbol *sym_Setter = NULL;
  77. struct symbol *sym_Singleton = NULL;
  78. struct symbol *sym_Subclass = NULL;
  79. struct symbol *sym_Throw = NULL;
  80. struct symbol *sym_Uwp = NULL;
  81. struct symbol *sym_Values = NULL;
  82. struct symbol *sym_Virtual = NULL;
  83.  
  84. static struct table {
  85.     int entries;
  86.     int threshold;
  87.     int length;
  88.     struct symbol **table;
  89. } Symbols;
  90.  
  91. static unsigned hash_name(char *name)
  92. {
  93.     unsigned char *ptr;
  94.     unsigned hash = 0;
  95.  
  96.     for (ptr = (unsigned char *)name; *ptr; ptr++)
  97.     hash = ((hash<<5)|(hash>>27)) ^ (*ptr & ~('a'^'A'));
  98.  
  99.     return hash;
  100. }
  101.  
  102. static boolean same_name(char *name1, char *name2)
  103. {
  104.     char c1, c2;
  105.  
  106.     while (1) {
  107.     c1 = *name1++;
  108.     c2 = *name2++;
  109.     
  110.     if (c1) {
  111.         if ((isupper(c1) ? tolower(c1) : c1)
  112.         != (isupper(c2) ? tolower(c2) : c2))
  113.         return FALSE;
  114.     }
  115.     else if (c2)
  116.         return FALSE;
  117.     else
  118.         return TRUE;
  119.     }
  120. }
  121.  
  122. static void rehash_table(struct table *table)
  123. {
  124.     int new_length;
  125.     struct symbol **new_table;
  126.     struct symbol **ptr;
  127.     int i;
  128.  
  129.     if (table->length < 1024)
  130.     new_length = table->length << 1;
  131.     else
  132.     new_length = table->length + 1024;
  133.  
  134.     new_table = malloc(sizeof(struct symbol *) * new_length);
  135.  
  136.     ptr = new_table;
  137.     for (i = 0; i < new_length; i++)
  138.     *ptr++ = NULL;
  139.  
  140.     ptr = table->table;
  141.     for (i = 0; i < table->length; i++) {
  142.     struct symbol *id, *next;
  143.     for (id = *ptr++; id != NULL; id = next) {
  144.         int index = id->hash % new_length;
  145.         next = id->next;
  146.         id->next = new_table[index];
  147.         new_table[index] = id;
  148.     }
  149.     }
  150.  
  151.     free(table->table);
  152.     table->table = new_table;
  153.     table->length = new_length;
  154.     table->threshold = (new_length * 3) / 2;
  155. }
  156.  
  157. static struct symbol *intern(char *name, struct table *table)
  158. {
  159.     unsigned hash = hash_name(name);
  160.     int index = hash % table->length;
  161.     struct symbol *id;
  162.  
  163.     for (id = table->table[index]; id != NULL; id = id->next)
  164.     if (id->hash == hash && same_name(name, (char *)id->name))
  165.         return id;
  166.  
  167.     id = malloc(sizeof(struct symbol) + strlen(name) + 1);
  168.     id->hash = hash;
  169.     id->next = table->table[index];
  170.     id->handle = -1;
  171.     table->table[index] = id;
  172.     strcpy((char *)id->name, name);
  173.     
  174.     table->entries++;
  175.     if (table->entries >= table->threshold)
  176.     rehash_table(table);
  177.  
  178.     return id;
  179. }
  180.  
  181. struct symbol *symbol(char *name)
  182. {
  183.     return intern(name, &Symbols);
  184. }
  185.  
  186. struct symbol *gensym(void)
  187. {
  188.     static int counter = 0;
  189.     static int rollover = 10;
  190.     static int digits = 1;
  191.  
  192.     struct symbol *res = malloc(sizeof(struct symbol) + 2 + digits);
  193.  
  194.     res->hash = (unsigned long)res;
  195.     res->next = NULL;
  196.     res->handle = -1;
  197.     sprintf((char *)res->name, "g%d", counter++);
  198.  
  199.     if (counter == rollover) {
  200.     digits++;
  201.     rollover *= 10;
  202.     }
  203.  
  204.     return res;
  205. }
  206.  
  207.  
  208. /* Init stuff. */
  209.  
  210. static void init_table(struct table *table)
  211. {
  212.     struct symbol **ptr;
  213.     int i;
  214.  
  215.     table->entries = 0;
  216.     table->threshold = 96;
  217.     table->length = 64;
  218.     table->table = (struct symbol **)malloc(sizeof(struct symbol *)*64);
  219.     ptr = table->table;
  220.     for (i = 0; i < 64; i++)
  221.     *ptr++ = NULL;
  222. }
  223.  
  224. void init_sym_table(void)
  225. {
  226.     init_table(&Symbols);
  227.  
  228.     sym_DefineClass1 = symbol("%define-class-1");
  229.     sym_DefineClass2 = symbol("%define-class-2");
  230.     sym_DefineGeneric = symbol("%define-generic");
  231.     sym_DefineMethod = symbol("%define-method");
  232.     sym_DefineSlot = symbol("%define-slot");
  233.     sym_Or = symbol("|");
  234.     sym_Plus = symbol("+");
  235.     sym_Less = symbol("<");
  236.     sym_LessEqual = symbol("<=");
  237.     sym_Object = symbol("<object>");
  238.     sym_Type = symbol("<type>");
  239.     sym_Eq = symbol("==");
  240.     sym_DylanUser = symbol("Dylan-User");
  241.     sym_Apply = symbol("apply");
  242.     sym_Aref = symbol("aref");
  243.     sym_Catch = symbol("catch");
  244.     sym_CheckType = symbol("check-type");
  245.     sym_Class = symbol("class");
  246.     sym_Constant = symbol("constant");
  247.     sym_Do = symbol("do");
  248.     sym_Element = symbol("element");
  249.     sym_Error = symbol("error");
  250.     sym_FindVariable = symbol("find-variable");
  251.     sym_ForwardIterationProtocol = symbol("forward-iteration-protocol");
  252.     sym_Getter = symbol("getter");
  253.     sym_InitVariable = symbol("init-variable");
  254.     sym_Instance = symbol("instance");
  255.     sym_List = symbol("list");
  256.     sym_MakeInherited = symbol("make-inherited");
  257.     sym_MakeInitarg = symbol("make-initarg");
  258.     sym_MakeNextMethodFunction = symbol("make-next-method-function");
  259.     sym_MakeSlot = symbol("make-slot");
  260.     sym_Negative = symbol("negative");
  261.     sym_NegativeP = symbol("negative?");
  262.     sym_NextMethod = symbol("next-method");
  263.     sym_PopHandler = symbol("pop-handler");
  264.     sym_PushHandler = symbol("push-handler");
  265.     sym_Setter = symbol("setter");
  266.     sym_Singleton = symbol("singleton");
  267.     sym_Subclass = symbol("subclass");
  268.     sym_Throw = symbol("throw");
  269.     sym_Uwp = symbol("uwp");
  270.     sym_Values = symbol("values");
  271.     sym_Virtual = symbol("virtual");
  272. }
  273.